import da from "element-ui/src/locale/lang/da";

class Sku {

    constructor(page = null) {
        this._data = {};
        this._def = [];//当前循环的参数值
        this.columnsData = [];//列表数据
        this.commission = 0;
    }

    setCommission(price){
        this.commission = price;
    }

    /**
     * 设置SKU 范围值
     * @param data
     */
    setSkuName(data) {
        this._data = data;
        //   this._value = [];
    }

    setSkuValue(data){
        this._value = data;
    }

    /**
     * 获取sku具体信息
     */
    getColumns() {
        this._def = [];
        this.columnsData = [];
        var forSize = 0,index = this._data.length - 1;
        for (var i = 0; i < this._data.length; i++) {
            var len = this._data[i].data.length;
            this._def.push(0);
            if(forSize == 0)
                forSize = len;
            else
                forSize = forSize * len;
        }
        for (var i = 0; i < forSize; i++) {
            this.inDef();
            this.defAdd(index);
        }


        var data = [];
        for (var i in this.columnsData) {
            var d = this.columnsData[i];
            var idAr = [];
            var ud = {};
            for(var i2 in d) {
                idAr.push(d[i2].subId);
            }
            ud.skuId = idAr.join(":");
            var sDk = this.findSkuId(ud.skuId);
            if(sDk == null) {
                var dk = this.findLikeSkuId(idAr);
                ud.price = 0;//原价
                ud.commission = dk != null ? dk.commission : this.commission;//售价
                ud.ot_price = 0;//成本价
                ud.stock = dk != null ? dk.stock : "1000";//人数
                ud.bar_code = "";//编码
                ud.weight = 0;//重量
                ud.img = "";
            } else {
                ud = sDk;
            }
            for(var i2 in d) {
                ud[d[i2].id] = d[i2].subName;
            }
            data.push(ud);
        }
        this._value = data;

        return data;
    }

    getValue(){
        var header = this._data;
        var data = this._value;
        var cloumn = {};
        for (var i in this.columnsData) {
            var d = this.columnsData[i];
            for(var i2 in d) {
                cloumn[d[i2].id] = 1;
            }
        }

        for(var i in data) {
            for(var j in data[i]) {
                if(cloumn[j] != null) {
                    delete data[i][j];
                }
            }
        }
        return {sku : JSON.stringify(header),skuValue : JSON.stringify(data)};
    }

    findSkuId(id){
        for(var i in this._value) {
            if(this._value[i].skuId == id) return this._value[i];
        }

        return null;
    }


    findLikeSkuId(idAr) {
        var valueAr = {};
        for(var i in idAr){
            for(var j in this._value) {
                var d = this._value[j].skuId;
                if(d.indexOf(idAr[i]) >= 0) {
                    if(valueAr[d] == null)
                        valueAr[d] = {count : 1,data:this._value[j]};
                    else
                        valueAr[d].count ++;
                }
            }
        }
        var dAr = {};
        for(var i in valueAr) {
            if(dAr.count == null || valueAr[i].count > dAr.count) {
                dAr = valueAr[i];
            }
        }
        return dAr.count == null ? null : dAr.data;
    }

    /**
     * 处理头部数据
     */
    inDef() {
        var data = [];
        for (var i in this._def) {
            var d = this._data[i];
            var sub = this._data[i].data[this._def[i]];
            data.push({
                name    : d.name,
                id      : d.id,
                subName : sub.name,
                subId   : sub.id
            });
        }
        this.columnsData.push(data);
    }

    /**
     * 循环点排序
     * @param index
     * @returns {undefined|*}
     * [0,0,0]  - [0,0,1]
     * [0,0,0]
     */
    defAdd(index) {
        if(index < 0) return;
        var defIndex = this._def[index];
        var defLen = this._data[index].data.length;
        if ((defIndex + 1) < defLen) {
            this._def[index]++;
        } else {
            this._def[index] = 0;
            index--;
            this.defAdd(index);
        }
    }


    /**
     * 生成唯一Id
     * @returns {undefined}
     */
    getId() {
        return Number(Math.random().toString().substr(3, 6) + Date.now()).toString(36);
    }

}


export default Sku
